package com.creditharmony.car.loan.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.creditharmony.car.common.base.bean.out.BaseDataOutBean;
import com.creditharmony.car.common.base.controller.GatewayBaseController;
import com.creditharmony.car.common.common.vo.requestvo.CarLoanFeeRequestVo;
import com.creditharmony.car.common.common.vo.responsevo.CarLoanFeeResponseVo;
import com.creditharmony.car.common.exception.UserException;
import com.creditharmony.car.common.utils.ErrorType;
import com.creditharmony.car.loan.service.CarLoanFeeService;
/**
 * 
 * @Class Name CarLoanFeeController
 * @author zqa
 * @Create In 2018年2月2日
 */
@RestController
@RequestMapping("/carLoanFee")
public class CarLoanFeeController extends GatewayBaseController {
    private static final Logger logger = LoggerFactory.getLogger(CarLoanFeeController.class);
    
    @Autowired
    private CarLoanFeeService carLoanFeeService;
    
    
    /**
     * 保存借款费用
     * 2018年2月2日
     * By zqa
     * @param vo 请求vo
     * @param result 验证信息
     * @param request 请求
     * @return BaseDataOutBean<CarLoanInfoResponseVo>
     */
    @CrossOrigin
    @RequestMapping(value = "saveLoanFee")
    public BaseDataOutBean<CarLoanFeeResponseVo> saveLoanFee(@Valid @RequestBody CarLoanFeeRequestVo vo,
            BindingResult result, HttpServletRequest request) {
        BaseDataOutBean<CarLoanFeeResponseVo> outBean = null;
        Map<String,List<String>> errorMsg = parseErrorMsg(result);
        if(errorMsg != null){
            outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.VALIDATE_ERROR);
            outBean.setErrorMsg(errorMsg);
            return outBean;
        }
        
        try {
            carLoanFeeService.saveCarLoanFee(vo);
            outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.NONE);
        } catch (Exception e) {
            if (e instanceof UserException) {
                outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.FAIL_BUSSINESS);
                logger.info(e.getMessage());
            } else {
                logger.info(ErrorType.ERROR_SYS_DB.getMessage());
                outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.ERROR_SYS);
            }
        }
        return outBean;
    }
    /**
     * 修改借款费用
     * 2018年2月2日
     * By zqa
     * @param vo 借款vo
     * @param result 验证
     * @param request 请求
     * @return BaseDataOutBean<CarLoanFeeResponseVo> 
     */
    @CrossOrigin
    @RequestMapping(value = "updateLoanFee")
    public BaseDataOutBean<CarLoanFeeResponseVo> updateLoanFee(@Valid @RequestBody CarLoanFeeRequestVo vo,
            BindingResult result, HttpServletRequest request) {
        BaseDataOutBean<CarLoanFeeResponseVo> outBean = null;
        Map<String,List<String>> errorMsg = parseErrorMsg(result);
        if(errorMsg != null){
            outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.VALIDATE_ERROR);
            outBean.setErrorMsg(errorMsg);
            return outBean;
        }
        
        try {
            carLoanFeeService.updateCarloanFee(vo);
            outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.NONE);
        } catch (Exception e) {
            if (e instanceof UserException) {
                outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.FAIL_BUSSINESS);
                logger.info(e.getMessage());
            } else {
                logger.info(ErrorType.ERROR_SYS_DB.getMessage());
                outBean = new BaseDataOutBean<CarLoanFeeResponseVo>(ErrorType.ERROR_SYS);
            }
        }
        return outBean;
    }
    
}
